package com.example.supernote.ui.trash

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.example.supernote.data.entity.FileEntity
import com.example.supernote.data.repository.FileEntityRepository
import com.example.supernote.model.manager.AbstractFileComposite
import com.example.supernote.model.manager.File
import com.example.supernote.model.manager.FileProperty
import com.example.supernote.model.manager.FileType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

data class TrashUiState(
    val trashDisplay: List<AbstractFileComposite> = listOf(),
    val searchPattern: String = "",
)

class TrashViewModel(
    private val trashRepository: FileEntityRepository
) : ViewModel() {
    private val _uiState = MutableStateFlow(TrashUiState())

    val uiState: StateFlow<TrashUiState> = _uiState.asStateFlow()

    init {
        viewModelScope.launch {
            trashRepository.getAllTrashEntitiesStream().collect { trashDisplay ->
                _uiState.update {
                    it.copy(trashDisplay = trashDisplay.map { file ->
                        println(file.id)
                        File(
                            property = FileProperty(
                                id = file.id,
                                name = file.name,
                                type = FileType.FILE,
                                lastModified = file.lastModified
                            )
                        )
                    })
                }
            }
        }
    }

    fun search(searchPattern: String) {
//        println(searchPattern)
        _uiState.update { it.copy(searchPattern = searchPattern) }
    }

    suspend fun recovery(file: AbstractFileComposite) {
        val id = file.property!!.id
        withContext(Dispatchers.IO) {
            val fileEntity = trashRepository.getTrashEntityStream(id)
            trashRepository.insertFileEntity(
                FileEntity(
                    id = fileEntity!!.id,
                    path = fileEntity.path,
                    name = fileEntity.name,
                    lastModified = fileEntity.lastModified,
                    text = fileEntity.text
                )
            )
            trashRepository.deleteTrashEntityById(id)
        }
    }

    suspend fun completelyDelete(file: AbstractFileComposite) {
        val id = file.property!!.id
        trashRepository.deleteTrashEntityById(id)
    }

    companion object {
        fun provideFactory(
            trashRepository: FileEntityRepository,
        ): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
            @Suppress("UNCHECKED_CAST")
            override fun <T : ViewModel> create(modelClass: Class<T>): T {
                return TrashViewModel(trashRepository) as T
            }
        }
    }
}